#include "rolleditdialog.h"
#include "ui_rolleditdialog.h"
#include "cadreroll.h"
#include <QSqlQuery>
#include <QSqlRecord>
#include <QMessageBox>
#include <QDebug>
#include <QSqlError>
#include <QSqlTableModel>
#include "cadreutil.h"
#include "timeeditdelegate.h"

RollEditDialog::RollEditDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::RollEditDialog)
{
    ui->setupUi(this);
    CadreRoll *parentwidget = (CadreRoll*)parentWidget();
    roll = parentwidget->currentRoll;
    initOther();
    initTable();
}

RollEditDialog::~RollEditDialog()
{
    delete ui;
}
void RollEditDialog::initTable()
{
    // init table
    QString sql = QString::fromUtf8("SELECT `CadreName`, `UsedName`, cc.`CredType`, ci.`IDNum` `IDNum` , "
                  "`Birth`, `JoinWorkTime`, `Sex`, `Health`, nationc.`Nation`, "
                  "`NativePlace`, `BirthPlace`, `GrowPlace`, politicc.`Politic`, "
                  "`PartyTime`,`DemoParty`, `RetireChange`, `Speciality`, "
                  "msc.`ManageStatus`, `IdenCate`, `ThreeOne`, `Phone`, "
                  "`SubstratumWork`, uc.unitname, rc.`RankName`, `RankTime`, "
                  "`RankSalary`, `Photo` "
                  "FROM ZZB.CadreRollRelation crr "
                  "left join ZZB.CadreInfo ci on crr.`IDNum`=ci.`IDNum` "
                  "left join ZZB.CredTypeCode cc on ci.`CredTypeID`= cc.`CredTypeID` "
                  "left join ZZB.NationCode nationc on nationc.`NationID`=ci.`NationID` "
                  "left join ZZB.PoliticCode politicc on politicc.`PoliticID`=ci.`PoliticID` "
                  "left join ZZB.ManageStatusCode msc on msc.`ManageStatusID`=ci.`ManageStatusID` "
                  "left join ZZB.unit uc on uc.`UnitID`=ci.`UnitID` "
                  "left join ZZB.RankCode rc on rc.`RankID`=ci.`RankID` "
                  "where `RollID`= %1").arg(roll);
    tableModel = new ResultTableModel(this);
    tableModel->setQuery(sql);
    tableModel->setHeaderData(0, Qt::Horizontal, QString::fromUtf8("姓名"));
    tableModel->setHeaderData(3, Qt::Horizontal, QString::fromUtf8("证件号码"));
    tableModel->setHeaderData(4, Qt::Horizontal, QString::fromUtf8("生日"));
    tableModel->setHeaderData(6, Qt::Horizontal, QString::fromUtf8("性别"));
    ui->cadreRollTable->setModel(tableModel);
    ui->cadreRollTable->horizontalHeader()->setVisible(true);
    TimeEditDelegate *birthtime = new TimeEditDelegate();
    ui->cadreRollTable->setItemDelegateForColumn(4,birthtime);
    tableModel->chash["Sex"]=6; //性别的列数
    for(int i=0;i< (tableModel->columnCount());i++){
        if(i==0|| i==3 || i==4 || i==6){
        }else{
            ui->cadreRollTable->setColumnHidden(i,true);
        }
    }
    ui->cadreRollTable->selectRow(tableModel->rowCount()-1);
    ui->cadreRollTable->show();
}
void RollEditDialog::initOther(){
    // init other;
    QSqlQuery query;
    query.exec(QString("SELECT `RollDate`, `RollName` FROM Roll Where RollID=%1").arg(roll));
    while(query.next()){
        ui->nameLineEdit->setText(query.value(1).toString());
        ui->rollDateEdit->setDate(query.value(0).toDate());
    }
}

void RollEditDialog::on_addButton_clicked()
{
    cadreSearchDialog = new RollCadreSearchDialog(this);
    cadreSearchDialog->exec();
}

void RollEditDialog::on_deleteButton_clicked()
{
    if(ui->cadreRollTable->rowAt(0)==-1){
        return;
    }
    int index = ui->cadreRollTable->currentIndex().row();
    QString selectIdNum = (tableModel->record(index)).value("IDNum").toString();
    QSqlQuery query;
    QString sql = QString::fromUtf8("delete from CadreRollRelation where RollID=:rollid "
                                    "and IDNum=:idnum");
    query.prepare(sql);
    query.bindValue(":rollid", roll);
    query.bindValue(":idnum", selectIdNum);
    if(query.exec()) {
        initTable();
    }else {
        QMessageBox::information(this,trUtf8("操作提示"),
            trUtf8("操作失败"),QMessageBox::Yes);
    }
}
// 查询页面确定返回后的操作
void RollEditDialog::receiveIdData(QStringList ids)
{
    if(!ids.isEmpty()){
         initTable();
    }
}

void RollEditDialog::on_saveButton_clicked()
{
    QSqlQuery query;
    QString sql = QString("UPDATE Roll SET `RollDate`=:rolldate, `RollName`=:rollname "
                          "WHERE `RollID`=:roll");
    query.prepare(sql);
    query.bindValue(":rolldate", ui->rollDateEdit->date());
    query.bindValue(":rollname", ui->nameLineEdit->text());
    query.bindValue(":roll", roll);
    if(query.exec()){
        CadreRoll *p =(CadreRoll*)parentWidget();
        p->initTableView(QStringList(),roll);
        this->reject();
        delete this;
    }else {
        qDebug()<<query.lastError();
    }
}

void RollEditDialog::on_cancelButton_clicked()
{
    this->reject();
    delete this;
}
QSqlTableModel* RollEditDialog::getOriginalModel()
{
    QSqlTableModel *originalmodel = new QSqlTableModel(this);
    originalmodel->setTable("CadreRollRelation");
    QString filter = QString::fromUtf8("RollID='%1'").arg(roll);
    originalmodel->setFilter(filter);
    originalmodel->select();
    originalmodel->setEditStrategy(QSqlTableModel::OnFieldChange);
    return originalmodel;
}

void RollEditDialog::on_upButton_clicked()
{
    QSqlTableModel *originalmodel = getOriginalModel();
    int cur = CadreUtil::moveUpOne(originalmodel,0,ui->cadreRollTable);
    initTable();
    ui->cadreRollTable->selectRow(cur);
}

void RollEditDialog::on_downButton_clicked()
{
    QSqlTableModel *originalmodel = getOriginalModel();
    int cur = CadreUtil::moveDownOne(originalmodel,0,ui->cadreRollTable);
    initTable();
    ui->cadreRollTable->selectRow(cur);
}
